package view.popup 
{
	import adobe.utils.CustomActions;
	import controller.Const;
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author Bim kute
	 */
	public class NoticePopup extends MovieClip 
	{
		[Embed(source="../../../lib/image/notice.png")]
		private var notice:Class;
		private var noticeBG:Bitmap = new notice();
		
		[Embed(source="../../../lib/image/dongy1.png")]
		private var dongy1:Class;
		[Embed(source = "../../../lib/image/dongy2.png")]
		private var dongy2:Class;
		[Embed(source="../../../lib/image/dong-nomal.png")]
		private var thoat1:Class;
		[Embed(source="../../../lib/image/dong-click.png")]
		private var thoat2:Class;
		
		
		private var mydongy1:Bitmap;
		private var mydongy2:Bitmap;
		private var mythoat1:Bitmap;
		private var mythoat2:Bitmap;
		
		private var _arrButt:Array;
		private var _noticeText:TextField;
		
		private var _shadow:Sprite;
		private var sp:Sprite;
		
		private var _all:Sprite;
		private var sp1:Sprite;
		private var sp2:Sprite;
		
		public function NoticePopup() 
		{
			_shadow = new Sprite();
			_shadow.graphics.beginFill(0x000000, .3);
			_shadow.graphics.drawRect(0, 0, 1499, 900);
			_shadow.graphics.endFill();
			addChild(_shadow);
			
			_all = new Sprite();
			addChild(_all);
			
			_arrButt = [];
			_all.addChild(noticeBG);
			noticeBG.x = (1400 - noticeBG.width) / 2;
			noticeBG.y = (900 - noticeBG.height) / 2 - 50;
			
			sp1 = new Sprite();
			_all.addChild(sp1);
			
			sp1.x = noticeBG.x + 40;
			sp1.y = noticeBG.y + 150;
			mydongy1 = new dongy1();
			sp1.addChild(mydongy1);
			mydongy2 = new dongy2();
			sp1.addChild(mydongy2);
			
			sp2 = new Sprite();
			_all.addChild(sp2);
			
			sp2.x = noticeBG.x + 160;
			sp2.y = noticeBG.y + 150;
			mythoat1 = new thoat1();
			sp2.addChild(mythoat1);
			mythoat2 = new thoat2();
			sp2.addChild(mythoat2);
			
			_arrButt = [sp1, sp2];
			
			mydongy1.visible = true;
			mydongy2.visible = false;
			mythoat1.visible = true;
			mythoat2.visible = false;
			
			sp1.buttonMode = sp2.buttonMode = true;
			
			_arrButt[0].addEventListener(MouseEvent.MOUSE_DOWN, onDownAgree);
			_arrButt[1].addEventListener(MouseEvent.MOUSE_DOWN, onDownCancel);
			
			_arrButt[0].addEventListener(MouseEvent.MOUSE_UP, onClickAgree);
			_arrButt[1].addEventListener(MouseEvent.MOUSE_UP, onClickCancel);
			
			_noticeText = new TextField();
			_noticeText.defaultTextFormat = new TextFormat("arial", 14, 0x241d06);
			_noticeText.text = "";
			_noticeText.x = noticeBG.x + 20;
			_noticeText.y = noticeBG.y + 40;
			_noticeText.width = 250;
			_noticeText.height = 250;
			_noticeText.multiline = true;
			_noticeText.wordWrap = true;
			_noticeText.mouseEnabled = false;
			_all.addChild(_noticeText);
			
			sp = new Sprite();
			sp.graphics.beginFill(0x123456, 1);
			sp.graphics.drawRect(0, 0, 1499, 900);
			sp.graphics.endFill();
			addChild(sp);
			mask = sp;
		}
		
		private function onDownAgree(e:MouseEvent):void 
		{
			mydongy1.visible = false;
			mydongy2.visible = true;
		}
		
		private function onDownCancel(e:MouseEvent):void 
		{
			mythoat1.visible = false;
			mythoat2.visible = true;
		}
		
		private function onClickAgree(e:MouseEvent):void 
		{
			mydongy1.visible = true;
			mydongy2.visible = false;
			if (_noticeText.text == Const.TEXT_BETTING) 
			{
				dispatchEvent(new Event("agreeBetting"));
			}
			
			else
			{
				dispatchEvent(new Event("close popup"));
			}
			
		}
		
		private function onClickCancel(e:MouseEvent):void 
		{
			dispatchEvent(new Event("close popup"));
		}
		
		/**
			 * 1: nut dong y hien
			 * 2: nut thoat hien
			 * 3 ca 2 nut cung hien
			 */
		private function showButton(type:int):void 
		{
			
			switch (type) 
			{
				case 1:
					_arrButt[1].visible = _arrButt[1].visible = false;
					_arrButt[0].visible = _arrButt[0].visible = true;
					_arrButt[0].visible = true;
					_arrButt[1].visible = false;
				break;
				case 2:
					//_arrButt[1].visible = _arrButt[1].visible = true;
					//_arrButt[0].visible = _arrButt[0].visible = false;
					_arrButt[0].visible = false;
					_arrButt[1].visible = true;
				break;
				case 3:
					_arrButt[1].visible = _arrButt[1].visible = true;
					_arrButt[0].visible = _arrButt[0].visible = true;
					_arrButt[0].visible = true;
					_arrButt[1].visible = true;
				break;
				
				default:
				_arrButt[1].visible = _arrButt[1].visible = true;
				_arrButt[0].visible = _arrButt[0].visible = true;
			}
		}
		
		public function addText(str:String):void 
		{
			trace(str);
			_noticeText.text = str;
			if (_noticeText.text == "Bạn có muốn thoát game không") 
			{
				_all.y = 0;
				sp.height = 768;
			}
			else 
			{
				_all.y = 0;
				sp.height = 768;
			}
			if (_noticeText.text == Const.TEXT_BETTING)
			{
				_all.y = 0;
				sp.height = 768;
			}
			
			var type:int = setTypeShowButton(str);
			
			showButton(type);
		}
		
		private function setTypeShowButton(str:String):int 
		{
			var type:int;
			
			switch (str) 
			{
				case Const.TEXT_BETTING:
					type = 3;
				break;
				
				case "Bạn đã nhẵn túi, hãy nạp tiền để tiếp tục săn đu đủ":
					type = 3;
				break;
				
				case "Số tiền bạn cược lớn hơn số tiền bạn đang có, xin vui lòng nạp thêm tiền vào tài khoản":
					type = 3;
				break;
				
				default:
					type = 2;
			}
			
			return type;
		}
		
		public function removeAllEvent():void
		{
			if (_shadow)
			{
				removeChild(_shadow);
				_shadow = null;
			}
			
			if (_arrButt && _arrButt.length > 0)
			{
				for (var i:int = 0; i < _arrButt.length; i++) 
				{
					_arrButt[i] = null;
				}
			}
			if (sp)
			{
				
				removeChild(sp);
				sp = null;
			}
			
			if (noticeBG)
			{
				_all.removeChild(noticeBG);
				noticeBG = null;
			}
			if (sp1)
			{
				//removeChild(sp1);
				sp1 = null;
			}
			if (sp2)
			{
				//removeChild(sp2);
				sp2 = null;
			}
			if (_all)
			{
				removeChild(_all);
				_all = null;
			}
			if (_noticeText)
			{
				_noticeText.text = "";
				//removeChild(_noticeText);
				_noticeText = null;
			}
		}
	}

}